<?php

namespace app\index\controller;

use app\common\respond\HttpApi;
use app\index\logic\UserLogic;

class UserController
{
    /**
     * @Description 注册
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/08/29 14:28:10
     * @return null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function register(HttpApi $respond)
    {
        $param = [
            'name' => request()->post('name'),
            'phone' => request()->post('phone'),
            'phone_code' => request()->post('phone_code') ?: '',
            'password' => request()->post('password'),
            'repassword' => request()->post('repassword'),
            'company' => request()->post('company'),
            'province' => request()->post('province'),
            'city' => request()->post('city'),
            'district' => request()->post('district'),
            'address' => request()->post('address')
        ];
        $rule = [
            'name' => 'require|length:2,20',
            'phone' => 'require|mobile',
            'phone_code' => 'max:6',
            'password' => 'require|length:4,20',
            'repassword' => 'require|confirm:password',
            'company' => 'require|max:50',
            'province' => 'require|max:15',
            'city' => 'require|max:15',
            'district' => 'require|max:15',
            'address' => 'require|max:50'
        ];

        validate($rule)->check($param);

        $logic = new UserLogic();
        $id = $logic->register($param['name'],$param['phone'],$param['password'],$param['phone_code'],$param['company'],$param['province'],$param['city'],$param['district'],$param['address']);

        return $respond->success([
            'id' => $id
        ]);
    }

    /**
     * @Description 注册验证码
     * @author: zj
     * @param HttpApi $respond
     * @time 2023/08/29 14:28:26
     * @return null
     */
    public function regCode(HttpApi $respond)
    {
        $param = [
            'phone' => request()->get('phone'),
            'code' => request()->get('code'),
        ];
        $rule = [
            'phone' => 'require|mobile',
            'code' => 'require',
        ];
        validate($rule)->check($param);

        $logic = new UserLogic();
        $logic->getRegPhoneCode($param['phone'], $param['code']);

        return $respond->success([]);
    }
}